from common.base_model import BaseModel
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone


# Create your models here.
class Order(BaseModel):
    """
    订单信息
    """
    # 订单状态
    statuses = (
        (0, '未下单'),     # 未提交订单
        (1, '待付款'),     # 下单了没有付款
        (2, '待发货'),     # 付款了没有发货
        (3, '已发货'),     # 已发货并邮寄出商品了
        (4, '已收货'),     # 卖家确定收货
    )

    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='用户信息')
    order_no = models.CharField(max_length=256, verbose_name='订单编号', null=True, blank=True)
    status = models.IntegerField(choices=statuses, verbose_name='订单状态', null=True, blank=True)
    # address = models.ForeignKey(Address, on_delete=models.CASCADE, verbose_name='配送地址')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单总金额', default=0)

    def __str__(self):
        return str(self.id)+str(self.order_no)

    class Meta:
        verbose_name_plural = '订单管理'
        db_table = 'tb_order'


class Payment(models.Model):
    """
    支付信息
    """
    # 支付状态
    statuses = (
        (0, '未支付'),
        (1, '已支付'),
    )

    # 支付方式
    methods = (
        (1, '微信'),
        (2, '支付宝'),
        (3, '建设银行储蓄卡'),
        (4, '工商银行储蓄卡')
    )

    id = models.AutoField(primary_key=True)
    order = models.OneToOneField(Order, on_delete=models.DO_NOTHING, verbose_name='需支付的订单')
    pay_time = models.DateTimeField(verbose_name='支付时间', default=timezone.now)
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='支付金额', default=0)
    status = models.IntegerField(choices=statuses, verbose_name='支付状态', default=0)
    payment_method = models.IntegerField(choices=methods, verbose_name='支付方式', default=2)

    def __str__(self):
        return str(self.id)

    class Meta:
        verbose_name_plural = '支付记录管理'
        db_table = 'tb_payment'
